# -*- coding: utf-8 -*-

'''eof
name:已结清借贷交易（中长期、短期、循环透支、贴现）关注类账户数
code:EC82_UnsettledConsAccCnt
tableName:
columnName:
groups:行内二代企业征信数据
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def isfloat(x):
    '''
    判断x为float类型且大于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) > 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def EC82_UnsettledConsAccCnt():
    data = EC_DSJ_2D.get('data')
    # 借贷交易汇总信息 ,出现次数[1..1],下级字段出现次数[1..1]
    EBB = getTable(data, 'EBB', 1, 1)
    if EBB == [{}]:
        return u'缺失值'
    elif EBB == 0:
        return 0
    else:
        #信贷交易汇总信息单元,出现次数[0..1]，下级字段出现次数[1..1]
        EB02 = getTable(EBB[0], 'EB02', 0, 1)
        if EB02 == [{}]:
            return u"缺失值"
        elif EB02 == 0:
            return 0
        else:
            #已结清借贷交易汇总信息段，出现次数[1..1]，下级字段出现次数[0..20]
            EB02B = getTable(EB02[0], 'EB02B', 1, 1)
            if EB02B == [{}]:
                return u"缺失值"
            elif EB02B == 0:
                return 0
            else:
                #其他借贷交易分类汇总信息，出现次数[0..20]，下级字段出现次数[1..1]
                EB02BH = getTable(EB02B[0], 'EB02BH', 0, 1)
                if EB02BH == [{}]:   
                    return u"缺失值"
                elif EB02BH == 0:
                    return 0
                else:
                    cont = 0
                    for i in EB02BH:
                        #业务类型
                        EB02BD01 = i.get("EB02BD01")
                        #资产质量分类
                        EB02BD02 = i.get("EB02BD02")
                        if EB02BD01 in [0,"0"] and EB02BD02 in [2,"2"]:
                            #账户数
                            EB02BS04 = i.get("EB02BS04")
                            if isfloat(EB02BS04):
                                return int(EB02BS04)
                                break
                            else:
                                cont += 1
                        else:
                            cont += 1
                    if cont == len(EB02BH):
                        return u"缺失值"

                
result = EC82_UnsettledConsAccCnt()
